home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / dir-ops.cc < prev    next >
C/C++ Source or Header  |  1996-11-10  |  2KB  |  115 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <cerrno>
  28. #include <cstring>
  29.  
  30. #include "sysdir.h"
  31.  
  32. #include "dir-ops.h"
  33. #include "str-vec.h"
  34.  
  35. bool
  36. dir_entry::open (const string& n)
  37. {
  38.   fail = true;
  39.  
  40.   if (! n.empty ())
  41.     name = n;
  42.  
  43.   if (! name.empty ())
  44.     {
  45.       close ();
  46.  
  47.       dir = (void *) opendir (name.c_str ());
  48.  
  49.       if (dir)
  50.     fail = false;
  51.       else
  52.     errmsg = strerror (errno);
  53.     }
  54.   else
  55.     errmsg = "dir_entry::open: empty file name";
  56.  
  57.   return ! fail;
  58. }
  59.  
  60. string_vector
  61. dir_entry::read (void)
  62. {
  63.   string_vector dirlist;
  64.  
  65.   if (ok ())
  66.     {
  67.       int count = 0;
  68.  
  69.       struct dirent *dir_ent;
  70.  
  71.       while ((dir_ent = readdir ((DIR *) dir)))
  72.     count++;
  73.  
  74.       rewinddir ((DIR *) dir);
  75.  
  76.       dirlist.resize (count);
  77.  
  78.       for (int i = 0; i < count; i++)
  79.     {
  80.       dir_ent = readdir ((DIR *) dir);
  81.  
  82.       if (dir_ent)
  83.         dirlist[i] = dir_ent->d_name;
  84.       else
  85.         break;
  86.     }
  87.     }
  88.  
  89.   return dirlist;
  90. }
  91.  
  92. void
  93. dir_entry::close (void)
  94. {
  95.   if (dir)
  96.     closedir ((DIR *) dir);
  97.  
  98.   dir = 0;
  99. }
  100.  
  101. void
  102. dir_entry::copy (const dir_entry& de)
  103. {
  104.   name = de.name;
  105.   dir = de.dir;
  106.   fail = de.fail;
  107.   errmsg = de.errmsg;
  108. }
  109.  
  110. /*
  111. ;;; Local Variables: ***
  112. ;;; mode: C++ ***
  113. ;;; End: ***
  114. */
  115.